<?php

/* 
 *手机端访问的控制器页面，这样就不会显得这么乱
 * 
 * 
 */
namespace Addons\Jinuser\Controller;
use  Addons\Jinuser\Controller\BaseController;    //后台分类的一些函数还是要用到，导入进来
use  Addons\Jinuser\Controller\WeiusercatController;   //导入用户远程操作

class WapuserController extends WeiusercatController{
    
       /*用户分类首页*/
     function index() {
                
		$map ['id'] = intval ( $_REQUEST ['id'] );
		$map ['token'] = get_token ();       //取公众号id
                $catlist = new BaseController();     //导入常用基类  
		$list = $catlist -> getcategory(1,2);    //获取分类，这里写试验中学，写死了以后有时间改,可以jinuser表里配置里去取的，传参数
		$question_id = I ( 'post.next_id', 0, 'intval' );            //下一题的id post
		if ($question_id == '-1') {
                   //如果是-1，表示是最后一步了，就去增加分组和处理数据库，最后部完成做
                    redirect ( U ( 'finish', 'id=' . $_REQUEST['score'] ) );
		}
             
		!($question_id !=0)&& $question_id=18;
                $rs = M('category')->find($question_id);
                $title = $rs['details'];
		if (empty ( $question_id )) {
			$question = $list [0];
			$next_id = isset ( $list [1] ['id'] ) ? $list [1] ['id'] : '-1';
		} else {
                        $extraArr = array();
			foreach ( $list as $k => $vo ) {
				if ($vo ['pid'] == $question_id) {
                                    $extraArr[$k]['title'] = $vo["title"];
                                    $extraArr[$k]['next_id'] = $vo ['id'];
                                    $extraArr[$k]['id'] = $vo ['id'];
                                    $next_id = $vo ['id'];
                                     /*往下走一步，如果没有了就把下一步斌值为-1*/
                                    foreach ($list as $v){
                                    if($v ['pid']== $next_id){
                                         $extraArr[$k]['next_id'] = $vo ['id'];
                                         break;
                                    }
                                    $extraArr[$k]['next_id'] = '-1';
                                    } 
				}
			}
                     
		}
                //这是第一次进来
           
                $acces_token = get_access_token ();
		if($question_id==18 && $acces_token!==0){
                    $this->assign ( 'title1', '欢迎您关注松滋市实验初级中学微信公众号！' );
                     //先找下默认用户数据，如果是未分组就提示分组
                    $opnid = get_openid();
                    $url = 'https://api.weixin.qq.com/cgi-bin/groups/getid?access_token='. $acces_token; 
                    $data = array('openid'=> $opnid);
                    $list = post_data($url, $data);     //查询用户所在分组结果，id不为0就不是默认分组，就显示信息
                    $woid = I("get.id");
                    if($list['groupid'] != 0 && $woid != 1){
                        $param =array();
                        $param ['token'] = get_token ();
                        $param ['openid'] = $opnid;
                        redirect ( U ( 'seekuser' ,$param) );
                    }
                    
                }  else {
                    $this->assign ( 'title1', '' );
                }
		$this->assign ( 'title', $title );
		$this->assign ( 'next_id', $next_id );
		$this->assign ( 'extra', $extraArr );
		$this-> assign ( 'system_copy_right',"松滋市实验初级中学");
		$this->display ();
	}
        
        



        //完成结果
        function finish() {
            $user = array();
	     $id = intval ( $_REQUEST ['id']);
             $title = M('category')->find($id);
             $user['title'] = $title['title'];
             $opnid = get_openid();
             $acces_token = get_access_token ();    //取系统的acc_token
             if($acces_token === 0){
                 $user['msg'] = '系统繁忙，请稍后再试';
                 $user['title'] = '系统繁忙，请稍后再试';
             } elseif ($opnid == -1) {
                   $user['msg'] = '您还未关注我们哦';
                   $user['title'] = '未关注友人，请返回微信先关注';
             } else {
                $fins =  $this ->getusercate($opnid ,$acces_token ,$user['title']);
                if(!$fins['errmsg'] != 'ok'){
                    $user['msg'] = '系统繁忙，请稍后再试';
                    $user['title'] = '系统繁忙，请稍后再试';
                }else {
                     $user['msg'] = '加入分组成功';
                     $user['title'] ='现在属于：'.$user['title'];
                }  
             }
            
             $this->assign('user', $user); 
             $this-> assign ( 'system_copy_right',"松滋市实验初级中学");
	     $this->display ();
	}
        
        //查询我的分组
        function seekuser(){
            $opnid = get_openid();
            $acces_token = get_access_token ();    //取系统的acc_token
             if($acces_token === 0){
                 $user['msg'] = '系统繁忙，请稍后再试';
                 $user['title'] = '系统繁忙，请稍后再试';
             } elseif ($opnid == -1) {
                   $user['msg'] = '您还未关注我们哦';
                   $user['title'] = '未关注友人，请返回微信先关注';
             }else{
                if(!S('weixincategory')){
                    $getmercory = $this ->getcategry($acces_token);
                    S('weixincategory', $getmercory, 3600);
                }
                $getmercory = S('weixincategory');
                $user = array();
                $getmercory = json_decode($getmercory, true);
                $getmercory = $getmercory['groups'];             //先取回总分组
                $url = 'https://api.weixin.qq.com/cgi-bin/groups/getid?access_token='. $acces_token; 
                $data = array('openid'=> $opnid);
                $data = JSON($data);
                $list = $this -> go_curl($url, 'POST', $data);     //查询用户所在分组结果
                $list = json_decode($list, true);
                $seek = strpos($list,'40003');
                if($seek){  
                     $user['msg'] = '您还未关注我们哦';
                     $user['title'] = '未关注友人，请返回微信先关注';
                }  else {
                     foreach ( $getmercory as $k => $v) { 

                        if($v['id']== $list['groupid']){
                           $user['title'] = $v["name"];

                          break ;
                        }   
                   }
                  $user['msg'] = '您当前所在分组';
                  $user['title'] ='现在属于：'.$user['title'];
            }
             }
             $param =array();
             $param['id'] = 1;
             $param ['token'] = get_token ();
             $param ['openid'] = $opnid;
             $url = addons_url ( 'Jinuser://Wapuser/index', $param );
             $this->assign('user', $user); 
             $this->assign('url',  $url ); 
             $this-> assign ( 'system_copy_right',"松滋市实验初级中学");
	     $this->display ();
        }


    
    
}